﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoadingBar
{
    public partial class MyLoadingBar : UserControl
    {
        private Timer animationTimer;
        private int scrollSpeed = 2; // 滚动速度
        private int direction = 1;   // 1 表示右移，-1 表示左移

        public MyLoadingBar()
        {
            InitializeComponent();
            InitializeAnimation();
            SetupDoubleBuffering();
        }

        private void SetupDoubleBuffering()
        {
            // 开启 Panel 双缓冲以减少闪烁
            trackPanel.DoubleBuffered(true);
        }

        private void InitializeAnimation()
        {
            // 初始化 Timer
            animationTimer = new Timer
            {
                Interval = 50 // 每 50ms 更新一次位置
            };
            animationTimer.Tick += AnimationTimer_Tick;

            // 默认垂直居中图片
            CenterPictureBoxVertically();

            // 启动动画
            //StartAnimation();
        }

        private void CenterPictureBoxVertically()
        {
            int y = (trackPanel.ClientSize.Height - indicator.Height) / 2;
            indicator.Location = new System.Drawing.Point(indicator.Left, y);
        }

        private void AnimationTimer_Tick(object sender, EventArgs e)
        {
            int panelWidth = trackPanel.ClientSize.Width;
            int pictureBoxWidth = indicator.Width;

            // 计算新的位置
            int newX = indicator.Left + scrollSpeed * direction;
            if (direction > 0)
            {
                // 检查是否到达边界
                if (newX > trackPanel.Width)
                {
                    indicator.Left = -indicator.Width;
                }
                else
                {
                    indicator.Left = newX;
                }
            }
            else
            {
                // 检查是否到达边界
                if (newX < -indicator.Width)
                {
                    indicator.Left = trackPanel.Width;
                }
                else
                {
                    indicator.Left = newX;
                }
            }
        }

        #region 公共方法和属性
        public void StartAnimation()
        {
            animationTimer.Start();
        }

        public void StopAnimation()
        {
            animationTimer.Stop();
            if (direction > 0)
            {
                indicator.Left = trackPanel.Width - indicator.Width;
            }
            else
            {
                indicator.Left = 0;
            }
        }

        public void SetDirection(int val)
        {
            direction = val;
        }

        /// <summary>
        /// 获取或设置 PictureBox 显示的图像
        /// </summary>
        public Image Image
        {
            get
            {
                return indicator.Image;
            }
            set
            { 
                indicator.Image = value; 
            }
        }

        /// <summary>
        /// 获取或设置滚动速度（1~20）
        /// </summary>
        public int ScrollSpeed
        {
            get
            {
                return scrollSpeed;
            }
            set
            {
                if (value < 1 || value > 20) return;
                scrollSpeed = value;
            }
        }
        #endregion
    }

    #region 扩展方法：开启双缓冲
    public static class ControlExtensions
    {
        public static void DoubleBuffered(this Control control, bool value = true)
        {
            var propInfo = control.GetType().GetProperty("DoubleBuffered",
                System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Instance);

            propInfo.SetValue(control, value, null);
        }
    }
    #endregion
}
